let jobaddModule = (function(){
    let $jobname = $('.jobname'),
        $jobdesc = $('.jobdesc'),
        $spanname = $('.spanname'),
        $spandesc = $('.spandesc'),
        $spanpower = $('.spanpower'),
        $submit = $('.submit'),
        $powlist = $('.power').find('input[type=checkbox]');
    let jobId = null;
    // 获取职务信息
    async function getJobInfo(){
        let result = await axios.get('/job/info',{
            params:{
                jobId
            }
        })
        if(result.code == 0){
            bindHTML(result.data);
        }
    }
    // 根据获得的职务信息 动态渲染到表单中
    function bindHTML(data){
        $jobname.val(data.name);
        $jobdesc.val(data.desc);
        [].forEach.call($powlist,item=>{
            if(data.power.includes($(item).val())){
                $(item).prop('checked',true)
            }
        })
    }
    // 表单校验
    function checkName(){
        let val = $jobname.val().trim();
        if(val === ''){
            $spanname.html('小主，职务名称不能为空~');
            return false;
        }else{
            $spanname.html('')
            return true;
        }
    }
    function checkPower(){
        let flag = $powlist.prop('checked');
        if(!flag){
            $spanpower.html('小主，您需要选中至少一项权限~');
            return false;
        }else{
            $spanpower.html('');
            return true;
        }
    }
    // 确认提交
    function handle(){
        $jobname.blur(checkName);
        $submit.click(async e=>{
            if(checkName() && checkPower()){
                // console.log($powlist.prop('checked'))// 有一个选中就为true
                let arr = [];
                [].forEach.call($powlist,item=>{
                    if($(item).prop('checked')){
                        arr.push($(item).val());
                    }
                })
                let params = {
                    name:$jobname.val().trim(),
                    desc:$jobdesc.val().trim(),
                    power: arr.join('|')
                }
                // 区分是编辑 还是 新增
                if(jobId){
                    params['jobId'] = jobId;
                    let result = await axios.post('/job/update',params);
                    if(result.code == 0){
                        alert('小主，已成功为您修改一个职务信息~');
                        return;
                    }
                    alert('小主，网络繁忙，请稍后重试');
                    return;
                }else{
                    let result = await axios.post('/job/add',params)
                    if(result.code == 0){
                        alert('小主，已成功为您新增一个职务信息~');
                        return;
                    }
                    alert('小主，网络繁忙，请稍后重试');
                    return;
                }
            }
        })
    }
   
    return {
        init(){
            let params = window.location.href.queryUrlParams();// 获取问号传参的值
            if(params.jobId){
                // 编辑页面
                jobId = params.jobId;
                console.log(jobId)
                getJobInfo();
            }
            handle();
        }
    }
}())
jobaddModule.init();